package easier.sectionheader.decoration;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.text.TextPaint;
import androidx.annotation.ColorInt;
import java.util.Objects;

public class CircleIndexer extends AbsSlideIndexer {

    protected Paint mPaintText;
    protected Paint mPaintBackground;

    public CircleIndexer( Builder builder ) {

        super( builder );
        mOutlinePaint.setStyle( Style.FILL );
        mOutlinePaint.setColor( Color.WHITE );
        mOutlinePaint.setShadowLayer( 5, 3, 3, Color.GRAY );

        mPaintText = new TextPaint( Paint.ANTI_ALIAS_FLAG );
        mPaintText.setTextSize( mIndexerTextSize );
        mPaintText.setColor( Color.GRAY );

        mPaintBackground = new Paint( Paint.ANTI_ALIAS_FLAG );
        mPaintBackground.setColor( Color.BLUE );
    }

    public CircleIndexer( Builder builder, @ColorInt int colorBackgroud, @ColorInt int indexTextColor ) {

        this( builder );

        mPaintBackground.setColor( colorBackgroud );
        mPaintText.setColor( indexTextColor );

        mIndexerTextPaint.setColor( Color.GRAY );
    }

    @Override
    public void drawIndicator( Canvas c, RectF outer, float indicatorBaseY, String indicatorChar ) {

        c.save();

        c.translate( mOutlineRect.left, mOutlineRect.top );

        // 2. Draw indexer.
        for ( int i = 0; i < mIndexerString.length(); i++ ) {
            String character = String.valueOf( mIndexerString.charAt( i ) );
            mIndexerTextPaint.getTextBounds( character, 0, character.length(), mTmpTextBound );
            float left = mCellWidth / 2.f - mTmpTextBound.width() / 2.f;
            float top = mCellHeight * (i + 1) + mTmpTextBound.height() / 2.f;
            if ( Objects.equals( indicatorChar.toUpperCase(), character.toUpperCase() ) ) {

                c.drawCircle( mCellWidth / 2.f, mCellHeight * (i + 1), mCellWidth * 0.45f, mPaintBackground );
                c.drawText( character, left, top, mPaintText );
            }
        }

        c.restore();
    }
}
